package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.pactera.madp.cp.api.dto.track.BehaviorTrackDto;
import com.pactera.madp.cp.api.em.workcontact.EventEnum;
import com.pactera.madp.cp.api.entity.BehaviorTrackEntity;
import com.pactera.madp.cp.common.constant.BehaviorTrackFormatConst;
import com.pactera.madp.cp.common.constant.Constant;
import com.pactera.madp.cp.service.IContactService;
import com.pactera.madp.cp.service.IWorkContactService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class BehaviorTrackShopingServiceImpl extends BaseBehaviorTrackServer {

    private IContactService contactService;

    @Override
    public void handleEvent(BehaviorTrackDto dto, IWorkEmployeeService workEmployeeService,
                            IWorkContactService workContactService, HttpServletResponse response) throws Exception{
        //参数校验
        vildateParams(dto);

        //事件处理
        BehaviorTrackEntity entity = new BehaviorTrackEntity();
        BeanUtil.copyProperties(dto, entity);
        baseHandleEvent(entity, workEmployeeService, workContactService, this, response);
        //轨迹记录
        saveTrack(entity);

        //链接重定向
        //response.sendRedirect(dto.getUrl());
//        log.info("URL--"+dto.getUrl());
    }

    /**
     * 参数校验
     * @param dto
     */
    private void vildateParams(BehaviorTrackDto dto) {
        if (StrUtil.isEmpty(dto.getLocation())) {
            throw new IllegalArgumentException("侧边栏位置不能为空");
        }
        if (StrUtil.isEmpty(dto.getUrl())) {
            throw new IllegalArgumentException("链接不能为空");
        }
    }

    /**
     * 业务数据组装
     * @param entity
     * @return
     */
    @Override
    protected String buildData(BehaviorTrackEntity entity){
        List<String> datas = new ArrayList<>();
        datas.add(entity.getUsername());
        datas.add(entity.getExternalUsername());
//        datas.add(entity.getLocation());
//        datas.add(entity.getTitle());
        datas.add(entity.getUrl());
        datas.add(String.valueOf(entity.getCorpId()));
        datas.add(String.valueOf(entity.getTenantId()));
        datas.add(String.valueOf(Constant.CUSTOM_DETAIL_AGENT_ID));
        datas.add(entity.getWxUserId());
        datas.add(entity.getWxExternalUserid());
        String content = String.format(BehaviorTrackFormatConst.FinancialShopFormat, datas.toArray(new String[datas.size()]));

        return content;
    }

    @SneakyThrows
    private void saveTrack(BehaviorTrackEntity entity) {
        Integer employeeId = entity.getEmployeeId();
        Integer contactId = entity.getContactId();
        if (employeeId == null || contactId == null || StringUtils.isAnyEmpty(
                entity.getExternalUsername(),
                entity.getUsername())
        ) {
            log.error("轨迹条件不满足：{}", entity.toString());
            return;
        }
        List<String> datas = new ArrayList<>();
        datas.add(entity.getExternalUsername());
        datas.add(entity.getUsername());
        String content = String.format(BehaviorTrackFormatConst.FinancialShopTrackFormat, datas.toArray(new String[datas.size()]));

        //保存轨迹 channel:2-侧边栏
        contactService.saveTrack(employeeId, contactId, EventEnum.SHOPING, content,2,entity.getCorpId());
    }

}
